<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生信息</title>
    <!-- 引入element-ui样式 -->
    <link rel="stylesheet" href="../../../plugins/element-ui/index.css">

    <!--    上传图片样式-->
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body>

<div id="app">
    <el-container style="height: 800px; border: 1px solid #eee">
        <el-container>
            <div style="width: 100%;position: relative">

                <div style=";margin-bottom: 80px;">
                    <el-input
                            placeholder="请输入学生姓名"
                            clearable
                            style="width: 20%; "
                            prefix-icon="el-icon-search"
                            v-model="Query.name"
                    >
                    </el-input>
                    <el-button @click="getAll()" class="dalfBut">查询</el-button>

                    <el-button type="warning" style="float: right" @click="Up_Add_dialogShow(null)">新增学生</el-button>
                </div>

                <template>
                    <el-table :data="dataList" border style="width: 100%">
                        <el-table-column fixed prop="stuNumber" label="学号" width="150" align="center"></el-table-column>
                        <el-table-column prop="password" label="密码" width="120" align="center"></el-table-column>
                        <el-table-column prop="name" label="姓名" width="120" align="center"></el-table-column>
                        <el-table-column prop="major" label="专业" width="120" align="center"></el-table-column>
                        <el-table-column prop="email" label="邮箱" width="300" align="center"></el-table-column>
                        <el-table-column prop="image" label="头像" width="120" align="center">
                            <template slot-scope="{ row }">
                                <img style="width: auto; height: 2px; border:none;cursor: pointer;"
                                     :preview-src-list=" `/files/downloadFile?name=${row.image}` "
                                     :src="getImage(row.image)"
                                />
                                <div slot="error" class="image-slot">
                                    <img :src="imageUrl1"  style="width: auto; height: 40px; border:none;" >
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="resume" label="个人简历" width="120" align="center"></el-table-column>
                        <el-table-column prop="college" label="学院" width="120" align="center"></el-table-column>

                        <el-table-column fixed="right" prop="status" label="状态" align="center" width="150" align="center">
                            <template slot-scope="scope">
                                <el-switch
                                        v-model="scope.row.status"
                                        active-text="启用"
                                        inactive-text="禁用"
                                        @change="updateStatus(scope.row)"
                                        active-value="true"
                                        inactive-value="false"
                                ></el-switch>
                            </template>
                        </el-table-column>

                        <el-table-column fixed="right" label="操作" width="200" align="center">
                            <template slot-scope="scope">
                                <el-button @click="down(scope.row)" type="text" size="small">下载简历</el-button>
                                <el-button  @click="Up_Add_dialogShow(scope.row)" type="text" size="small">编辑</el-button>
                                <el-button @click="DeleteStudent(scope.row)" type="text" size="small">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>


                <!--分页组件-->
                <el-pagination
                        class="pagination"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>


                <!--2.修改 学生弹框-->
                <el-dialog :title="title" style="height: 650px"  :visible.sync="dialog_update">
                    <el-form :model="formData_update" :rules="rules" ref="formData_update" label-width="100px" class="demo-formData_update">
                        <el-form-item  label="学号：" prop="stuNumber">
                            <el-input v-model="formData_update.stuNumber"></el-input>
                        </el-form-item>

                        <el-form-item  label="密码：" prop="password">
                            <el-input v-model="formData_update.password"></el-input>
                        </el-form-item>

                        <el-form-item  label="姓名：" prop="name">
                            <el-input v-model="formData_update.name"></el-input>
                        </el-form-item>

                        <el-form-item  label="专业" prop="major">
                            <el-input v-model="formData_update.major"></el-input>
                        </el-form-item>

                        <el-form-item  label="邮箱：" prop="email">
                            <el-input v-model="formData_update.email"></el-input>
                        </el-form-item>


                        <el-form-item  label="院校：" prop="college">
                            <el-input v-model="formData_update.college"></el-input>
                        </el-form-item>

                        <el-form-item  label="照片：" prop="image">
                            <el-upload class="avatar-uploader"
                                       action="/files/uploadFile"
                                       :show-file-list="false"
                                       :on-success="handleAvatarSuccess"
                                       :before-upload="beforeUpload"
                                       ref="upload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>



                        <el-form-item>
                            <el-button type="primary" @click="UpdateStudent">确定修改</el-button>
                            <el-button @click="center">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>

                <!--3.新增 学生弹框-->
                <el-dialog :title="title" style="height: 650px"  :visible.sync="dialog_add">
                    <el-form :model="formData_add" :rules="rules" ref="formData_add" label-width="100px" class="demo-formData_add">
                        <el-form-item  label="学号：" prop="stuNumber">
                            <el-input v-model="formData_add.stuNumber"></el-input>
                        </el-form-item>

                        <el-form-item  label="密码：" prop="password">
                            <el-input v-model="formData_add.password"></el-input>
                        </el-form-item>

                        <el-form-item  label="姓名：" prop="name">
                            <el-input v-model="formData_add.name"></el-input>
                        </el-form-item>

                        <el-form-item  label="专业" prop="major">
                            <el-input v-model="formData_add.major"></el-input>
                        </el-form-item>

                        <el-form-item  label="邮箱：" prop="email">
                            <el-input v-model="formData_add.email"></el-input>
                        </el-form-item>

                        <el-form-item  label="院校：" prop="college">
                            <el-input v-model="formData_add.college"></el-input>
                        </el-form-item>

                        <el-form-item  label="照片：" prop="image">
                            <el-upload class="avatar-uploader"
                                       action="/files/uploadFile"
                                       :show-file-list="false"
                                       :on-success="handleAvatarSuccess"
                                       :before-upload="beforeUpload"
                                       ref="upload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>


                        <el-form-item>
                            <el-button type="primary" @click="AddStudent">确定添加</el-button>
                            <el-button @click="center">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>


            </div>
        </el-container>
    </el-container>
</div>


<script src="../../../plugins/vue/vue.js"></script>

<!-- 引入element-ui组件库 -->
<script src="../../../plugins/element-ui/index.js"></script>

<!-- 引入axios -->
<script src="../../../plugins/axios/axios.min.js"></script>

<script>

    new Vue({
        el:'#app',    //element
        data:{

            studentInfo:{}, //当前用户信息

            //搜索内容
            Query:{
                name:""
            },

            //显示的数据
            dataList:[],

            //element-ui分页插件
            pagination: {//分页相关模型数据 1
                currentPage: 1,//当前页码1
                pageSize:5,//每页显示的记录数1
                total:0,//总记录数1
            },

            //状态
            status: false,

            //修改弹框内容
            formData_update:{
                image:'',
            },
            dialog_update:false,


            //新增弹框内容
            formData_add:{
                image:'',
            },
            dialog_add:false,

            //文件上传
            fileList: [
                {name: '', url: ''}
            ],

             imageUrl:''

        },

        created() {
            // const studentInfo = window.localStorage.getItem('student')
            // console.log("studentInfo1:::"+studentInfo)
            // if (studentInfo) {
            //     this.studentInfo = JSON.parse(studentInfo)
            // }
            // console.log("studentInfo2:::"+studentInfo)

            this.getAll();

        },
        methods: {

            getAll() {
                parent = "?name=" + this.Query.name;
                axios.get("/students/getPageStudent/" + this.pagination.currentPage + "/" + this.pagination.pageSize + parent).then((res) => {
                    console.log(res)
                    this.dataList = res.data.data.records
                    this.pagination.total = res.data.data.total
                    this.Query.name = ""
                })
            },

            //切换页码
            handleCurrentChange(currentPage) {
                console.log("切换页码:")
                this.pagination.currentPage = currentPage;
                console.log(currentPage)
                this.getAll();
            },


            //修改用户状态
            updateStatus(row) {
                axios.put("/students/updateStudentStatus", row).then((res) => {

                })
            },
            //修改弹框的显示和隐藏
            Up_Add_dialogShow(row){
                //弹框显示时,先清空弹框
                console.log(row)
                if (row!==null) {
                    //数据回显
                    this.formData_update = row
                    this.imageUrl = `/files/downloadFile?name=${row.image}`

                    //弹框显示
                    this.dialog_update=true
                }else {
                    this.imageUrl = ""
                    this.formData_add={}
                    this.dialog_add=true
                }
            },
            //确定添加
            AddStudent(){
                axios.post("/students/addStudent",this.formData_add).then((res)=> {
                    if (res.data.code){
                        this.$message.success(res.data.msg)
                    }else{
                        this.$message.error(res.data.msg)
                    }
                }).finally(()=>{
                    this.getAll();
                    this.dialog_add=false;
                })
            },

            //确定修改
            UpdateStudent(){

                axios.put("/students/updateStudent",this.formData_update).then((res)=>{     //修改数据回显
                    if (res.data.code){
                        // alert("删除成功")
                        this.$message.success(res.data.msg)
                    }else{
                        this.$message.error(res.data.msg)
                    }
                }).finally(()=>{
                    this.getAll();
                    this.dialog_update=false;
                })
            },


            //通过id删除
            DeleteStudent(row){
                this.$confirm("此操作删除当学生全部信息，是否继续?","提醒",{type: 'info'
                }).then(()=>{
                    axios.delete("/students/deleteStudent/"+row.id).then((res)=> {
                        console.log("deleteById:")
                        console.log(res)
                        if (res.data.code){
                            this.$message.success(res.data.msg)
                        }else{
                            this.$message.error(res.data.msg)
                        }
                    }).finally(()=>{
                        this.getAll()
                    })
                }).catch(()=>{
                    //取消删除
                    this.$message.info("取消删除操作")
                })
            },

            center() {
                this.dialog_update=false
                this.dialog_add=false
                this.$message.info("取消操作")
                this.getAll()
            },


            //4.图片文件上传
            handleAvatarSuccess (response, file, fileList) {
                console.log("response:")
                console.log(response)

                this.formData_add.image=response.data
                this.formData_update.image=response.data
                this.imageUrl = `/files/downloadFile?name=${response.data}`
                console.log(this.imageUrl)
            },
            beforeUpload (file) {
                if(file){
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 2
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 2MB!')
                        return false
                    }
                    return file
                }
            },
            getImage (image) {
                this.imageUrl1= `/files/downloadFile?name=${image}`
            },

            //简历文件下载
            down(row){
                // alert("qwe")
                window.location.href="http://localhost:8080/files/downloadFile2?name="+row.resume
            },
        }
    })

</script>
</body>
</html>




